#--------------------------------------------------------
# Stage 1: Install apps that cannot be installed via apt
#--------------------------------------------------------
FROM ubuntu:24.04 AS builder

ADD apps/lazygit_linux_amd64.tar.gz /usr/local/bin/
ADD apps/zoxide_linux_amd64.tar.gz /usr/local/bin/
ADD apps/fzf_linux_amd64.tar.gz /usr/local/bin/
ADD apps/nvim_linux_amd64.tar.gz /usr/local/bin/

#--------------------------------------------------------
# Stage 2: Use an official Ubuntu LTS image
#--------------------------------------------------------
FROM ubuntu:24.04

LABEL Author="Arthur Sun" Email="shouxing.sun@aliyun.com"

# Arguments and Environments
ARG LANGUAGE=en_US.UTF-8
ARG USERNAME=admin

# Using apt-get with a domestic mirror source in China
# COPY ubuntu.sources /etc/apt/sources.list.d/

# Install desired packages
RUN apt-get update && \
    apt-get install --assume-yes \
        locales \
        build-essential \
        sudo \
        git \
        git-flow \
        zsh \
        tmux \
        curl \
        eza \
        bat \
        ranger \
        iputils-ping \
        ripgrep \
        && \
    rm -rf /var/lib/apt/lists/*

# Generate locales
RUN locale-gen $LANGUAGE

# Copy apps from builder of Stage 1
COPY --from=builder /usr/local/bin/nvim-linux-x86_64/ /usr/local/
COPY --from=builder \
    /usr/local/bin/lazygit \
    /usr/local/bin/zoxide \
    /usr/local/bin/fzf \
    /usr/local/bin/

# Create a non-root user and set it as default
RUN groupadd --gid 2000 $USERNAME && \
    useradd --uid 2000 --gid 2000 --create-home --shell /usr/bin/zsh $USERNAME && \
    echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME && \
    chmod 0440 /etc/sudoers.d/$USERNAME
USER $USERNAME

# Clone dotfiles
RUN git clone --recursive https://gitee.com/arthur_sun/dotfiles.git /home/$USERNAME/.config/ && \
    ln -s /home/$USERNAME/.config/zshrc /home/$USERNAME/.zshrc

# Set default working directory (optional)
WORKDIR /home/$USERNAME